package com.flow.modules.dingtalk.service.impl;

import com.dingtalk.api.response.OapiAttendanceListRecordResponse;
import com.flow.modules.dingtalk.service.DingTalkAttendanceService;
import com.flow.modules.dingtalk.service.DingTalkService;
import com.flow.modules.sys.service.SysDeptService;
import com.flow.modules.sys.service.SysUserService;
import com.google.common.collect.Lists;
import dm.jdbc.util.StringUtil;
import liquibase.pro.packaged.A;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author liuledong [1125615798@qq.com]
 * @version V1.0
 * @description: TODO
 * @date 2023/5/6 14:45
 */
@Slf4j
@Service
public class DingTalkAttendanceServiceImpl implements DingTalkAttendanceService {

    @Autowired
    private SysUserService sysUserService;


    @Autowired
    private DingTalkService dingTalkService;


    public List<List<OapiAttendanceListRecordResponse>> getAttendanceInfoByDepts(String accessToken, List<String> deptList,
                                                                                 String checkDateFrom, String checkDateTo) {
        try {
            List<List<OapiAttendanceListRecordResponse>> list = new ArrayList<>();
            for (int i = 0; i < deptList.size(); i++) {
                String depts = deptList.get(i);
                List<List<String>> userIdsGroup = getUserIdsGroup(depts);
                List<OapiAttendanceListRecordResponse> attendanceInfoByDeptsList = getAttendanceInfoByDeptsList(accessToken, userIdsGroup, checkDateFrom, checkDateTo);
                list.add(Optional.ofNullable(attendanceInfoByDeptsList).orElse(Lists.newArrayList()));
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 一个部门一个大集合
     *
     * @param accessToken
     * @param userIds
     * @param checkDateFrom
     * @param checkDateTo
     * @return List<OapiAttendanceListRecordResponse>
     */
    public List<OapiAttendanceListRecordResponse> getAttendanceInfoByDeptsList(String accessToken, List<List<String>> userIds,
                                                                               String checkDateFrom, String checkDateTo) {
        try {
            List<OapiAttendanceListRecordResponse> list = new ArrayList<>();
            Optional.ofNullable(userIds).orElse(Lists.newArrayList()).stream().forEach(userIdList -> {
                OapiAttendanceListRecordResponse attendanceByUserIds = dingTalkService.getAttendanceByUserIds(accessToken, userIdList, checkDateFrom, checkDateTo);
                list.add(attendanceByUserIds);
            });
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 50人一组按部门分
     * 一个部门返回一次集合
     *
     * @param deptId
     * @return List<List < String>>
     */
    public List<List<String>> getUserIdsGroup(String deptId) {
        try {
            List<String> userIds = sysUserService.getUserIdsByDeptIds(deptId);
            return Optional.ofNullable(groupListByQuantity(userIds, 50)).orElse(Lists.newArrayList());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将集合按指定数量分组
     *
     * @param list
     * @param quantity
     * @return 分组结果
     */
    public <T> List<List<T>> groupListByQuantity(List<T> list, int quantity) {
        if (list == null || list.size() == 0) {
            return null;
        }
        if (quantity <= 0) {
            throw new IllegalArgumentException("Wrong quantity.");
        }
        List<List<T>> wrapList = new ArrayList<List<T>>();
        int count = 0;
        while (count < list.size()) {
            wrapList.add(new ArrayList<T>(list.subList(count, Math.min((count + quantity), list.size()))));
            count += quantity;
        }
        return wrapList;
    }

}
